<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 职位查询 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button>
                <el-select v-model="query.did" placeholder="部门" class="handle-select mr10">
                    <el-option label="全部部门" value=""></el-option>
                    <el-option :key="index" :label="item.name" v-for="(item, index) in depts" :value="item.id"></el-option>
                </el-select>
                <el-select v-model="query.status" placeholder="状态" class="handle-select mr10">
                    <el-option key="1" label="全部状态" value=""></el-option>
                    <el-option key="2" label="正常" value="01"></el-option>
                    <el-option key="3" label="冻结" value="00"></el-option>
                </el-select>
                <el-select v-model="query.level" placeholder="等级" class="handle-select mr10">
                    <el-option label="等级" value=""></el-option>
                    <el-option :key="index" :label="item.name" v-for="(item, index) in levels" :value="item.code"></el-option>
                </el-select>
                <el-input v-model="query.name" placeholder="职位名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column type="index" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="did" label="职位部门" :formatter="deptFormat"></el-table-column>
                <el-table-column prop="leaderId" label="上级" :formatter="leaderFormat"></el-table-column>
                <el-table-column prop="name" label="职位名称"></el-table-column>
                <el-table-column prop="description" label="描述"></el-table-column>
                <el-table-column label="状态">
                    <template slot-scope="scope">
                        <el-switch
                            active-color="#13ce66"
                            inactive-color="#ff4949"
                            :value="scope.row.status"
                            :active-value="'01'"
                        ></el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="350px">
                    <template slot-scope="scope">
                        <el-button type="success" size="mini" icon="el-icon-view" @click="view(scope.row.id)">详情</el-button>
                        <el-button type="primary" size="mini" icon="el-icon-edit" @click="edit(scope.row.id)">编辑</el-button>
                        <el-button type="danger" size="mini" icon="el-icon-close" @click="del(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageNum"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
import { fetchData } from '../../../api/index';
export default {
    name: 'basetable',
    inject: ['reload'],
    data() {
        return {
            query: {
                did: '',
                leaderId: '',
                level: '',
                status: '',
                name: '',
                pageNum: 1,
                pageSize: 5
            },
            levels: [
                { name: '董事长', code: '01' },
                { name: '副董事长', code: '02' },
                { name: '经理', code: '03' },
                { name: '组长', code: '04' },
                { name: '员工', code: '05' }
            ],
            depts: [],
            tableData: [],
            positions: [],
            multipleSelection: [],
            delList: [],
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getPositions();
        this.getDepts();
        this.initPosition();
    },
    methods: {
        async getPositions() {
            var d = await this.$http.get('/positions');
            this.positions = d.data;
        },
        // 格式化部门一列
        deptFormat(row, column) {
            var did = row[column.property];
            if (did == '00') {
                return '暂无部门';
            }
            if (did == '01') {
                return '**';
            }
            for (var i = 0; this.depts.length > i; i++) {
                if (this.depts[i].id == did) {
                    return this.depts[i].name;
                }
            }
        },
        // 格式化上级一列
        leaderFormat(row, column) {
            var lid = row[column.property];
            if (lid == '00') {
                return '暂无领导';
            }
            if (lid == '01') {
                return '**';
            }
            for (var i = 0; this.positions.length > i; i++) {
                if (this.positions[i].id == lid) {
                    return this.positions[i].name;
                }
            }
        },
        // 获取 easy-mock 的模拟数据
        async initPosition() {
            var d = await this.$http.post('/positions', this.query);
            if (d.code == 200) {
              if(d.data!=null){
                this.tableData = d.data.list;
                this.pageTotal = d.data.total;
              }else{
                  this.tableData=[]
              }
            
            } else {
                this.$message.error(d.message);
            }
        },
        async getDepts() {
            var d = await this.$http.get('/depts');
            if (d.code == 200) {
                this.depts = d.data;
            } else {
                this.depts = ['--获取信息失败--'];
            }
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.initPosition();
        },
        // 编辑操作
        view(id) {
            this.$router.push({ path: '/getPosition', query: { id: id } });
        },
        // 编辑操作
        edit(id) {
            this.$router.push({ path: '/updatePosition', query: { id: id } });
        },
        // 删除操作
        del(id) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(async () => {
                    var d = await this.$http.delete('/position/' + id);
                    if (d.code == 200) {
                        this.$message.success(d.message);
                        this.reload();
                    } else{
                        this.$message.error(d.message);
                    }
                })
                .catch(() => {});
        },

        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },

        //批量删除
        async delAllSelection() {
            const length = this.multipleSelection.length;
            var delName = '';
            for (let i = 0; i < length; i++) {
                this.delList.push(this.multipleSelection[i].id);
                delName = delName + this.multipleSelection[i].name + ' ';
            }
            this.$confirm('确定要删除 ' + delName + '吗？', '提示', {
                type: 'warning'
            })
                .then(async () => {
                    var d = await this.$http.post('/delpositions', this.delList);
                    if (d.code == 200) {
                        this.$message.success(d.message);
                        this.multipleSelection = [];
                        this.reload();
                    }else {
                        this.$message.error(d.message);
                    }
                })
                .catch(() => {});
        },
        // 分页导航
        handlePageChange(val) {
            this.query.pageNum = val;
            this.initPosition();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
